<template>
    <div class="resume-forum-container bg-[#f1f3f6]">
        <!-- 页面头部 -->

        <header
            class="relative overflow-hidden bg-gradient-to-br from-indigo-600 via-purple-600 to-pink-600 rounded-2xl text-white mb-8 shadow-2xl">
            <div class="absolute inset-0 bg-gradient-to-r from-black/10 to-transparent" />
            <div class="relative px-8 py-12">
                <div class="flex flex-col lg:flex-row justify-between items-start lg:items-center gap-6">
                    <div class="space-y-4">
                        <h1
                            class="text-5xl font-bold tracking-tight bg-gradient-to-r from-white to-blue-100 bg-clip-text text-transparent">
                            简历管理
                        </h1>
                        <p class="text-xl opacity-90 max-w-2xl leading-relaxed">
                            制作你的第一份求职简历
                        </p>

                        <button
                            class="group flex items-center gap-3 px-8 py-4 bg-white/10 backdrop-blur-sm border border-white/20 rounded-xl text-white font-semibold hover:bg-white/20 hover:border-white/30 hover:-translate-y-1 transition-all duration-300 shadow-lg"
                            @click="createNewResume">
                            <svg class="w-5 h-5 transition-transform duration-300 group-hover:rotate-90"
                                viewBox="0 0 24 24" fill="none" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                    d="M12 4v16m8-8H4" />
                            </svg>
                            新建简历
                        </button>
                    </div>
                </div>
            </div>
        </header>

        <!-- 简历列表 -->
        <div class="resume-list-container">
            <div v-if="loading" class="loading-container">
                <div class="loading-spinner"></div>
                <p>加载中...</p>
            </div>

            <div v-else-if="resumes.length === 0" class="empty-state">
                <div class="empty-icon">📄</div>
                <h3>暂无简历</h3>
                <p>创建您的第一份简历，开始求职之旅</p>
                <button class="create-first-btn" @click="createNewResume">
                    创建简历
                </button>
            </div>

            <div v-else class="resume-grid">
                <div v-for="resume in resumes" :key="resume.id" class="resume-card" @click="editResume(resume.id)">
                    <div class="card-header">
                        <h3 class="resume-title">{{ resume.title }}</h3>
                        <div class="card-actions">
                            <button class="action-btn edit-btn" @click.stop="editResume(resume.id)" title="编辑">
                                <i class="icon-edit"></i>
                            </button>
                            <button class="action-btn delete-btn" @click.stop="deleteResume(resume.id)" title="删除">
                                <i class="icon-delete"></i>
                            </button>
                        </div>
                    </div>

                    <div class="card-content">
                        <div class="resume-preview">
                            <div v-if="getResumePreview(resume)" class="preview-info">
                                <div class="preview-name">{{ getResumePreview(resume)?.name || '未填写姓名' }}</div>
                                <div class="preview-contact">
                                    <span v-if="getResumePreview(resume)?.phone" class="contact-item">
                                        📞 {{ getResumePreview(resume)?.phone }}
                                    </span>
                                    <span v-if="getResumePreview(resume)?.email" class="contact-item">
                                        ✉️ {{ getResumePreview(resume)?.email }}
                                    </span>
                                </div>
                                <div v-if="getResumePreview(resume)?.jobIntention" class="preview-job">
                                    🎯 {{ getResumePreview(resume)?.jobIntention }}
                                </div>
                            </div>
                            <div v-else class="preview-empty">
                                <p>简历内容为空</p>
                            </div>
                        </div>
                    </div>

                    <div class="card-footer">
                        <div class="resume-meta">
                            <span class="update-time">
                                <i class="icon-time"></i>
                                {{ formatDate(resume.updatedAt) }}
                            </span>
                            <span class="resume-id">ID: {{ resume.id }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 删除确认对话框 -->
        <div v-if="showDeleteDialog" class="modal-overlay" @click="closeDeleteDialog">
            <div class="modal-content" @click.stop>
                <h3>确认删除</h3>
                <p>确定要删除这份简历吗？此操作不可撤销。</p>
                <div class="modal-actions">
                    <button class="cancel-btn" @click="closeDeleteDialog">取消</button>
                    <button class="confirm-btn" @click="confirmDelete" :disabled="deleting">
                        {{ deleting ? '删除中...' : '确认删除' }}
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { resumeApi } from '~/utils/api'
import type { ResumeItem, ResumeContent } from '~/types/resume'

// 添加认证中间件
definePageMeta({
    middleware: 'auth'
})

// 响应式数据
const resumes = ref<ResumeItem[]>([])
const loading = ref(false)
const showDeleteDialog = ref(false)
const deleting = ref(false)
const deleteResumeId = ref<number | null>(null)

// 获取简历列表
const fetchResumes = async () => {
    try {
        loading.value = true
        const response = await resumeApi.getResumesApi()
        if (response.success) {
            resumes.value = response.data || []
        } else {
            console.error('获取简历列表失败:', response.message)
        }
    } catch (error) {
        console.error('获取简历列表失败:', error)
    } finally {
        loading.value = false
    }
}

// 获取简历预览信息
const getResumePreview = (resume: ResumeItem): ResumeContent | null => {
    try {
        if (resume.content) {
            return JSON.parse(resume.content) as ResumeContent
        }
    } catch (error) {
        console.error('解析简历内容失败:', error)
    }
    return null
}

// 格式化日期
const formatDate = (date: Date | string) => {
    const d = new Date(date)
    return d.toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
    })
}

// 创建新简历
const createNewResume = () => {
    navigateTo('/StudyArea/Resume/ResumeDetail/new')
}

// 编辑简历
const editResume = (id: number) => {
    navigateTo(`/StudyArea/Resume/ResumeDetail/${id}`)
}

// 删除简历
const deleteResume = (id: number) => {
    deleteResumeId.value = id
    showDeleteDialog.value = true
}

// 关闭删除对话框
const closeDeleteDialog = () => {
    showDeleteDialog.value = false
    deleteResumeId.value = null
}

// 确认删除
const confirmDelete = async () => {
    if (!deleteResumeId.value) return

    try {
        deleting.value = true
        const response = await resumeApi.deleteResumeApi(deleteResumeId.value)
        if (response.success) {
            // 从列表中移除已删除的简历
            resumes.value = resumes.value.filter(r => r.id !== deleteResumeId.value)
            console.log('简历删除成功')
        } else {
            console.error('删除简历失败:', response.message)
        }
    } catch (error) {
        console.error('删除简历失败:', error)
    } finally {
        deleting.value = false
        closeDeleteDialog()
    }
}

// 页面加载时获取简历列表
onMounted(() => {
    fetchResumes()
})
</script>

<style scoped>
.resume-forum-container {
    padding: 20px;
    max-width: 1200px;
    margin: 0 auto;
    min-height: calc(100vh - 100px);
}

.forum-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding-bottom: 20px;
    border-bottom: 2px solid #e8e8e8;
}

.forum-title {
    font-size: 28px;
    font-weight: 600;
    color: #333;
    margin: 0;
}

.header-actions {
    display: flex;
    gap: 12px;
}

.create-btn {
    background: #1890ff;
    color: white;
    border: none;
    border-radius: 6px;
    padding: 10px 20px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.3s;
}

.create-btn:hover {
    background: #40a9ff;
    transform: translateY(-1px);
}

.create-btn .icon-plus::before {
    content: '+';
    font-size: 16px;
    font-weight: bold;
}

.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    color: #666;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #1890ff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 16px;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.empty-state {
    text-align: center;
    padding: 80px 20px;
    color: #666;
}

.empty-icon {
    font-size: 64px;
    margin-bottom: 20px;
}

.empty-state h3 {
    font-size: 20px;
    margin-bottom: 12px;
    color: #333;
}

.empty-state p {
    margin-bottom: 24px;
    color: #999;
}

.create-first-btn {
    background: #1890ff;
    color: white;
    border: none;
    border-radius: 6px;
    padding: 12px 24px;
    font-size: 16px;
    cursor: pointer;
    transition: all 0.3s;
}

.create-first-btn:hover {
    background: #40a9ff;
}

.resume-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 20px;
}

.resume-card {
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: all 0.3s;
    cursor: pointer;
    overflow: hidden;
}

.resume-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.card-header {
    padding: 20px 20px 16px;
    border-bottom: 1px solid #f0f0f0;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.resume-title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin: 0;
    flex: 1;
    line-height: 1.4;
}

.card-actions {
    display: flex;
    gap: 8px;
    opacity: 0;
    transition: opacity 0.3s;
}

.resume-card:hover .card-actions {
    opacity: 1;
}

.action-btn {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s;
}

.edit-btn {
    background: #1890ff;
    color: white;
}

.edit-btn:hover {
    background: #40a9ff;
}

.delete-btn {
    background: #ff4d4f;
    color: white;
}

.delete-btn:hover {
    background: #ff7875;
}

.action-btn .icon-edit::before {
    content: '✏️';
    font-size: 14px;
}

.action-btn .icon-delete::before {
    content: '🗑️';
    font-size: 14px;
}

.card-content {
    padding: 16px 20px;
    min-height: 120px;
}

.resume-preview {
    height: 100%;
}

.preview-info {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.preview-name {
    font-size: 16px;
    font-weight: 600;
    color: #333;
}

.preview-contact {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.contact-item {
    font-size: 14px;
    color: #666;
    display: flex;
    align-items: center;
    gap: 6px;
}

.preview-job {
    font-size: 14px;
    color: #1890ff;
    font-weight: 500;
}

.preview-empty {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #999;
    font-style: italic;
}

.card-footer {
    padding: 12px 20px;
    background: #fafafa;
    border-top: 1px solid #f0f0f0;
}

.resume-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    color: #999;
}

.update-time {
    display: flex;
    align-items: center;
    gap: 4px;
}

.update-time .icon-time::before {
    content: '🕒';
    font-size: 12px;
}

.resume-id {
    font-family: monospace;
    background: #f0f0f0;
    padding: 2px 6px;
    border-radius: 4px;
}

/* 模态框样式 */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-content {
    background: white;
    border-radius: 12px;
    padding: 24px;
    max-width: 400px;
    width: 90%;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.modal-content h3 {
    margin: 0 0 12px 0;
    font-size: 18px;
    color: #333;
}

.modal-content p {
    margin: 0 0 24px 0;
    color: #666;
    line-height: 1.5;
}

.modal-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
}

.cancel-btn,
.confirm-btn {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s;
}

.cancel-btn {
    background: #f5f5f5;
    color: #666;
}

.cancel-btn:hover {
    background: #e8e8e8;
}

.confirm-btn {
    background: #ff4d4f;
    color: white;
}

.confirm-btn:hover:not(:disabled) {
    background: #ff7875;
}

.confirm-btn:disabled {
    background: #d9d9d9;
    cursor: not-allowed;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .resume-forum-container {
        padding: 16px;
    }

    .forum-header {
        flex-direction: column;
        gap: 16px;
        align-items: stretch;
    }

    .forum-title {
        font-size: 24px;
        text-align: center;
    }

    .resume-grid {
        grid-template-columns: 1fr;
        gap: 16px;
    }

    .card-actions {
        opacity: 1;
    }
}
</style>